from tortoise import fields, signals
from .base import BaseModel, TimestampMixin
from .enums import StationType, AGVStatus


class Map_Node_Mo(BaseModel, TimestampMixin):
    """
    站点模型 - 存储AGV系统中的工作站、充电站等固定位置信息
    """
    name = fields.CharField(unique=True, max_length=50, description="站点名称")
    type = fields.CharEnumField(StationType, description="站点类型")
    status = fields.CharEnumField(AGVStatus, description="站点状态")
    x = fields.IntField(description="X坐标位置")
    y = fields.IntField(description="Y坐标位置")
    description = fields.CharField(max_length=255, null=True, description="站点描述")

    class Meta:
            table = "map_nodes"
            table_description = "AGV系统地图节点表"

class Map_Edge_Mo(BaseModel, TimestampMixin):
    """
    地图边模型 - 存储AGV系统中的地图边信息
    """
    node_from = fields.ForeignKeyField("models.Map_Node_Mo", related_name="edges_from", description="起始节点ID")
    node_to = fields.ForeignKeyField("models.Map_Node_Mo", related_name="edges_to", description="目标节点ID")
    weight = fields.IntField(null=True, description="权重")
    occupied = fields.IntField(null=True, description="是否被占用")
    distance = fields.IntField(null=True, description="距离")
    description = fields.CharField(max_length=255, null=True, description="边描述")

    class Meta:
            table = "map_edges"
            table_description = "AGV系统地图边表"

# class Station_Charge_Mo(Station_Mo):
#     """
#     充电站模型 - 继承自Station_Mo，添加充电站特定属性
#     """
#     number = fields.IntField(unique=True, null=True, description="充电站编号")
#     current = fields.IntField(null=True, description="电流")
#     voltage = fields.IntField(null=True, description="电压")
#     power = fields.IntField(null=True, description="功率")
    
#     class Meta:
#         table = "station_charge"
#         table_description = "AGV系统充电站表"


# class Station_location_Mo(Station_Mo):
#     """
#     入库站模型 - 继承自Station_Mo，添加入库站特定属性
#     """
#     number = fields.IntField(null=True,  description="入库站编号")
#     isOnfull = fields.IntField(null=True, description="是否有货物")
#     status = fields.CharField(max_length=20, null=True, description="入库站状态")

#     class Meta:
#         table = "station_location"
#         table_description = "AGV系统库位表"


# def get_target_model(instance: Station_Mo):
#     """根据Station_Mo实例的type获取对应的表模型"""
#     model_map = {
#         StationType.CHARGING: Station_Charge_Mo,
#         StationType.INBOUND: Station_location_Mo,
#         StationType.OUTBOUND: Station_location_Mo,
#         StationType.SORTING: Station_location_Mo,
#         StationType.MAINTENANCE: Station_location_Mo,
#         StationType.UNKNOWN: Station_location_Mo,
#     }
#     return model_map.get(instance.type)


# @signals.post_save(Station_Mo)
# async def sync_save_or_update(sender, instance, created, using_db=None, update_fields=None, **kwargs):
#     # 获取对应的子表模型
#     target_model = get_target_model(instance)
#     if not target_model:
#         raise ValueError(f"未知的类型: {instance.type}")

#     # 准备基础字段数据 - 不包含id字段
#     base_fields = {
#         "created_at": instance.created_at,
#         "updated_at": instance.updated_at,
#         "name": instance.name,
#         "type": instance.type,
#         "x": instance.x,
#         "y": instance.y,
#         "description": instance.description,
#     }

#     # 根据不同的子表添加特定字段的默认值
#     if target_model == Station_Charge_Mo:
#         base_fields["number"] = instance.id * 10
#     elif target_model == Station_location_Mo:
#         base_fields["number"] = instance.id * 5

#     if created:
#         # 创建记录时需要指定id
#         create_fields = base_fields.copy()
#         create_fields["id"] = instance.id
#         await target_model.create(**create_fields)
#     else:
#         # 更新记录时不包含id字段，只更新需要的字段
#         # 如果有update_fields参数，只更新这些字段
#         if update_fields:
#             update_data = {k: v for k, v in base_fields.items()
#                             if k in update_fields}
#         else:
#             update_data = base_fields

#         await target_model.filter(id=instance.id).update(**update_data)


# @signals.post_delete(Station_Mo)
# async def sync_delete(sender, instance, using_db=None, **kwargs):
#     # 获取对应的子表模型
#     target_model = get_target_model(instance)
#     if not target_model:
#         raise ValueError(f"未知的类型: {instance.type}")

#     # 删除子表记录
#     await target_model.filter(id=instance.id).delete()
